﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace Login
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void txtName_KeyDown(object sender, KeyEventArgs e)
       {
            if (e.KeyCode == Keys.F4 && e.Modifiers == Keys.Alt)
                Application.Exit(); 
        }

        private void txtpassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F4 && e.Modifiers == Keys.Alt)
                Application.Exit(); 
        }

        private void btnOK_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F4 && e.Modifiers == Keys.Alt)
                Application.Exit(); 
        }

        private void btnCancel_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F4 && e.Modifiers == Keys.Alt)
                Application.Exit(); 
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            SqlConnection con = Program.GetConnection;
            SqlDataReader dr = null;
            try
            {
                SqlCommand cmd = new SqlCommand("SELECT * FROM Users WHERE LoginName ='" +
                    txtName.Text + "' AND Password='" + txtpassword.Text +"'", con);
                dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    Program.UserLoginName = dr.GetString(3);
                    this.Close();
                }
                else
                    MessageBox.Show("Invalid Username and Password!"); 
            }
            catch ( Exception ex)
            { 
                MessageBox.Show(ex.Message);
            }
        }

        private void Login_Load(object sender, EventArgs e)
        {
            this.AcceptButton = btnOK;
        }
    }
}
